<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateMessagesTable extends Migration
{
    private $tableName = 'messages';
    private $tableComment = '聊天消息表';
    private $primaryKey = 'id';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->increments(env('DB_CONNECTION') == 'oracle' ? strtoupper($this->primaryKey) : $this->primaryKey)->comment('商品id');
            $table->unsignedInteger('uid')->comment('发送消息用户id')->default(0);
            $table->unsignedInteger('fid')->comment('签到分数')->default(0);
            $table->mediumText('content')->comment('积分记录类型 1：签到，2兑换')->nullable();
            $table->unsignedInteger('type')->comment('消息类型 0：type:1文字，type:2图片，type:3音频 ')->default(1);
            $table->unsignedInteger('uread_type')->comment('是否已读 0：未读，1已读')->default(0);
            $table->unsignedInteger('fread_type')->comment('是否已读 0：未读，1已读')->default(0);
            $table->timestamps();

            if (env('DB_CONNECTION') == 'oracle') {
                $table->comment = $this->tableComment;
            }
        });

        if (env('DB_CONNECTION') == 'mysql') {
            DB::statement("ALTER TABLE `" . DB::getTablePrefix() . $this->tableName . "` comment '{$this->tableComment}'");
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
        if (env('DB_CONNECTION') == 'oracle') {
            $sequence = DB::getSequence();
            $sequence->drop(strtoupper($this->tableName . '_' . $this->primaryKey . '_SEQ'));
        }
    }
}
